﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchEdu.Framework.DataModel;
using SchEdu.Framework.Exceptions;
using SchEdu.Framework.Logging;

namespace SchEdu.Framework.Constraint
{
    public class Constrainter
    {
        private Logger logger = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());

        protected Logger Logger
        {
            get { return logger; }
            set { logger = value; }
        }

        SchEduData schEduData;

        public SchEduData SchEduData
        {
            get { return schEduData; }
            set { schEduData = value; }
        }

        SchEduDataSet.SolutionRow solution;

        public SchEduDataSet.SolutionRow Solution
        {
            get { return solution; }
            set { solution = value; }
        }


        ConstraintCollection constraints = new ConstraintCollection();
        public ConstraintCollection Constraints
        {
            set
            {
                constraints = value;
            }
            get
            {
                return constraints;
            }
        }
        public Constrainter(SchEduData schEduData, SchEduDataSet.SolutionRow solution)
        {
            this.schEduData = schEduData;
            this.solution = solution;
        }

        public void Validate(SchEduDataSet.SolutionRow solution)
        {

            foreach (SchEduDataSet.AllocationRow allocation in solution.GetAllocationRows())
            {
                foreach (SchEduDataSet.ConstraintRow constraint in allocation.GetConstraintRows())
                {
                    constraint.Delete();
                }
            }

            foreach (IConstraint constraint in constraints)
            {
                foreach (SchEduDataSet.AllocationRow allocation in solution.GetAllocationRows())
                {
                    try
                    {
                        constraint.SchEduData = schEduData;
                        constraint.Solution = solution;
                        constraint.Validate();
                    }
                    catch (ValidatorException ex)
                    {
                        continue;
                    }
                    catch (ConstraintException ex)
                    {
                        SchEduDataSet.ConstraintRow constraintRow = schEduData.Constraint.NewConstraintRow();
                        constraintRow.AllocationID = allocation.ID;
                        constraintRow.Message = ex.Message;
                        constraintRow.Title = constraint.AboutInfo.Title;
                        constraintRow.Version = constraint.AboutInfo.Version;
                        constraintRow.Company = constraint.AboutInfo.Company;
                        constraintRow.Copyright = constraint.AboutInfo.Copyright;
                        constraintRow.Description = constraint.AboutInfo.Description;
                        constraintRow.Product = constraint.AboutInfo.Product;
                        schEduData.Constraint.AddConstraintRow(constraintRow);
                    }
                }
            }
        }
    }
}
